package cn.bob.org.controller;

import cn.bob.basic.query.NovelQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Novel;
import cn.bob.org.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/novel")
public class NovelController {

    @Autowired
    private NovelService novelService;

    // 添加或修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Novel novel){
        System.out.println(novel);
        if (novel.getId() == null){
            novel.setCreatetime(new Date());
            novelService.addNovel(novel);
        } else {
            novel.setCreatetime(new Date());
            novelService.updateNovel(novel);
        }
        return AjaxResult.me().success();
    }
    // 查询所有
    @GetMapping
    public List<Novel> findAll(){
        return novelService.findAll();
    }
    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        novelService.delNovel(id);
        return AjaxResult.me().success();
    }


    // 单条语句查询
    @GetMapping("/{id}")
    public Novel findById(@PathVariable("id") Long id){
        return novelService.findById(id);
    }
    // 分页查询
    @PostMapping
    public PageList<Novel> queryByPage(@RequestBody NovelQuery novelQuery){
        System.out.println(novelQuery);
        return novelService.queryByPage(novelQuery);
    }

}
